package com.rustic.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rustic.server.dao.NationalityDictionaryDao;
import com.rustic.server.po.NationalityDictionary;
import com.rustic.server.query.NationalityDictionaryAdminListQuery;
import com.rustic.server.service.NationalityDictionaryService;
import com.rustic.server.vo.list.NationalityDictionaryAdminListVO;
import com.rustic.server.vo.list.NationalityDictionaryWebListVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * NationalityDictionary对应的服务类实现
 *
 * @author auto
 */

@Service
public class NationalityDictionaryServiceImpl extends ServiceImpl<NationalityDictionaryDao, NationalityDictionary> implements NationalityDictionaryService {

    @Override
    public List<NationalityDictionary> listByIdIn(List<Integer> idList) {
        if (CollUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        return this.listByIds(idList);
    }

    @Override
    public Map<Integer, NationalityDictionary> mapByIdIn(List<Integer> idList) {
        List<NationalityDictionary> nationalityDictionaryList = this.listByIdIn(idList);
        return nationalityDictionaryList.stream().collect(Collectors.toMap(NationalityDictionary::getId, b -> b));
    }

    @Override
    public LambdaQueryWrapper<NationalityDictionary> toAdminListWrapper(NationalityDictionaryAdminListQuery query) {
        LambdaQueryWrapper<NationalityDictionary> queryWrapper = new LambdaQueryWrapper<>();
        //FIXME 根据需要修改搜索条件
        queryWrapper.eq(query.getId() > 0, NationalityDictionary::getId, query.getId());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getCode()), NationalityDictionary::getCode, query.getCode());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getCodeSimple()), NationalityDictionary::getCodeSimple, query.getCodeSimple());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getName()), NationalityDictionary::getName, query.getName());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getNameEn()), NationalityDictionary::getNameEn, query.getNameEn());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getMobilePrefix()), NationalityDictionary::getMobilePrefix, query.getMobilePrefix());
        queryWrapper.eq(query.getSort() > 0, NationalityDictionary::getSort, query.getSort());
        return queryWrapper;
    }

    @Override
    public List<NationalityDictionaryAdminListVO> toAdminListVo(List<NationalityDictionary> nationalityDictionaryList) {
        List<NationalityDictionaryAdminListVO> results = new ArrayList<>();
        for (NationalityDictionary nationalityDictionary : nationalityDictionaryList) {
            NationalityDictionaryAdminListVO nationalityDictionaryAdminListVO = new NationalityDictionaryAdminListVO(nationalityDictionary);
            results.add(nationalityDictionaryAdminListVO);
        }
        return results;
    }

    @Override
    public List<NationalityDictionaryWebListVO> toWebListVo(List<NationalityDictionary> nationalityDictionaryList) {
        List<NationalityDictionaryWebListVO> results = new ArrayList<>();
        for (NationalityDictionary nationalityDictionary : nationalityDictionaryList) {
            NationalityDictionaryWebListVO nationalityDictionaryWebListVO = new NationalityDictionaryWebListVO(nationalityDictionary);
            results.add(nationalityDictionaryWebListVO);
        }
        return results;
    }
}
